package com.tianhui.consignortwofei.mvp.ui.activity.audit.vehicle;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.fgs.common.CommonFragment;
import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonView;
import com.fgs.common.entity.PictureInfo;
import com.fgs.common.utils.DateUtil;
import com.fgs.common.widget.SelectDialog;
import com.fgs.common.widget.itemView.ClickItemView;
import com.fgs.common.widget.itemView.InputItemView;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.constant.DictionaryType;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.mvp.model.enty.DictionaryInfo;
import com.tianhui.consignortwofei.mvp.model.enty.SelectPhotoInfo;
import com.tianhui.consignortwofei.mvp.model.enty.VehicleInfo;
import com.tianhui.consignortwofei.mvp.model.enty.authLicense.VehicleLicenseInfo;
import com.tianhui.consignortwofei.other.auth.PictureContext;
import com.tianhui.consignortwofei.other.auth.inte.SelectPhotoCallback;
import com.tianhui.consignortwofei.other.auth.stragety.VehicleBackStrategy;
import com.xuexiang.xui.widget.picker.widget.TimePickerView;
import com.xuexiang.xui.widget.picker.widget.builder.TimePickerBuilder;
import com.xuexiang.xui.widget.picker.widget.listener.OnTimeSelectListener;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class VehicleBackAuditActivity extends BaseVehicleAuditActivity {


    @BindView(R.id.activity_vehicle_back_audit_plateNumberInputItemView)
    InputItemView mPlateNumberInputItemView;
    @BindView(R.id.activity_vehicle_back_audit_weightInputItemView)
    InputItemView mWeightInputItemView;
    @BindView(R.id.activity_vehicle_back_audit_totalWeightInputItemView)
    InputItemView mTotalWeightInputItemView;
    @BindView(R.id.activity_vehicle_back_audit_endDateClickItemView)
    ClickItemView mEndDateClickItemView;
    @BindView(R.id.activity_vehicle_back_audit_engineTypeClickItemView)
    ClickItemView mEngineTypeClickItemView;

    private TimePickerView mEndDatePickerView;
    private SelectDialog mVehicleEnergyTypeDialog;
    private String mBackImageId;
    private String mEngineType;

    @Override
    public int getLayoutId() {
        return R.layout.activity_vehicle_back_audit;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }

    @Override
    public CommonFragment createFragment() {
        return null;
    }

    @Override
    public void changePictureSuccess(PictureInfo pictureInfo) {
        mBackImageId = pictureInfo.id;
    }

    @Override
    public void showVehicleInfo(VehicleInfo info) {
        mPlateNumberInputItemView.setContent(info.vehiclenum);
        mWeightInputItemView.setContent(info.carrying);
        mTotalWeightInputItemView.setContent(info.fullcarrying);
        mEngineTypeClickItemView.setContent(info.vehicleenergytypename);
        mEndDateClickItemView.setContent(info.effectivedate);
        loadPicture(info.imgdrivingimagecopy);
        mBackImageId = info.drivingimagecopy;
        mEngineType = info.vehicleenergytype;

    }

    @Override
    public VehicleInfo getSaveInfo() {
        String weight = mWeightInputItemView.getContent();
        String totalWeight = mTotalWeightInputItemView.getContent();
        String endDate = mEndDateClickItemView.getContent();
        String engineTypeName = mEngineTypeClickItemView.getContent();
        String engineType = mEngineType;
        if (TextUtils.isEmpty(weight)) {
            showToast("请输入车辆载质量");
            return null;
        }
        if (TextUtils.isEmpty(totalWeight)) {
            showToast("请输入满载车辆载质量");
            return null;
        }
        if (TextUtils.isEmpty(endDate)) {
            showToast("请选择驾驶证有效期");
            return null;
        }
        if (TextUtils.isEmpty(engineTypeName)) {
            showToast("请选择车辆能源类型");
            return null;
        }
        mVehicleInfo.carrying = weight;
        mVehicleInfo.fullcarrying = totalWeight;
        mVehicleInfo.effectivedate = endDate;
        mVehicleInfo.drivingimagecopy = mBackImageId;
        mVehicleInfo.vehicleenergytype = engineType;
        mVehicleInfo.vehicleenergytypename = engineTypeName;
        return mVehicleInfo;
    }

    @Override
    public void nextAudit() {
        VehicleInfo vehicleInfo = getSaveInfo();
        if (vehicleInfo != null) {
            Bundle bundle = new Bundle();
            bundle.putParcelable(KeyValue.KEY_VEHICLE_INFO, vehicleInfo);
            bundle.putInt(KeyValue.KEY_OPERATE,getIntent().getExtras().getInt(KeyValue.KEY_OPERATE));
//            openActivityForResult(VehicleTowedFrontAuditActivity.class, bundle);
            openActivityForResult(VehicleBack2AuditActivity.class, bundle);
        }
    }

    //注册日期
    @OnClick(R.id.activity_vehicle_back_audit_endDateClickItemView)
    public void selectEndDate() {
        hideKeyboard();
        if (mEndDatePickerView == null) {
            mEndDatePickerView = new TimePickerBuilder(this, new OnTimeSelectListener() {
                @Override
                public void onTimeSelected(Date date, View v) {
                    String string = DateUtil.getStandardTime(date.getTime(), "yyyy-MM-dd");
                    mEndDateClickItemView.setContent(string);
                }
            })
                    .setTitleText("请选择日期")
                    .build();
        }
        mEndDatePickerView.show();
    }

    //车辆能源类型
    @OnClick(R.id.activity_vehicle_back_audit_engineTypeClickItemView)
    public void selectEngineType() {
        hideKeyboard();
        if (null == mVehicleEnergyTypeDialog) {
            Map<String, String> map = new HashMap<>();
            map.put("pid", "2505");
            requestTypeList(DictionaryType.TYPE_VEHICLE_ENERGY, map, true);
        } else {
            mVehicleEnergyTypeDialog.show();
        }
    }

    @Override
    public void showTypeList(int type, List<DictionaryInfo> list) {
        switch (type) {
            case DictionaryType.TYPE_VEHICLE_ENERGY:
                mVehicleEnergyTypeDialog = new SelectDialog(this);
                showSelectDialog(DictionaryType.TYPE_VEHICLE_ENERGY, "请选择车辆能源类型", mVehicleEnergyTypeDialog, mEngineTypeClickItemView, list);
                break;
        }

    }

    @Override
    public void setTypeInfo(int type, com.fgs.common.interf.ISelectItemValue iSelectItemValue) {
        switch (type) {
            case DictionaryType.TYPE_VEHICLE_ENERGY:
                mEngineType = iSelectItemValue.getSelectItemTextValue();
                break;
        }
    }

    @OnClick(R.id.layout_audit_picture_editImageView)
    public void changePicture() {
        VehicleBackStrategy vehicleBackStrategy = (VehicleBackStrategy) pictureStrategyFactory.createStrategy(VehicleBackStrategy.class);
        pictureContext = new PictureContext(vehicleBackStrategy);
        pictureContext.selectPhoto(getContext(), new SelectPhotoCallback<VehicleLicenseInfo>() {
            @Override
            public void selectPhotoSuccess(SelectPhotoInfo<VehicleLicenseInfo> resultInfo) {
                mBackImageId = resultInfo.pictureId;
                loadPicture(resultInfo.realFileName);
                processVehicleBackResult(resultInfo.resultInfo);
            }

            @Override
            public void selectPhotoFailed() {

            }
        });
    }

    private void processVehicleBackResult(VehicleLicenseInfo resultInfo) {
        if (resultInfo == null) return;
        mWeightInputItemView.setContent(resultInfo.loadQuality);
        mTotalWeightInputItemView.setContent(resultInfo.totalMass);
        mEndDateClickItemView.setContent(resultInfo.indate);
    }
}
